<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');
//用户表
class Users_model extends CI_Model{
    function __construct() {
        parent::__construct();
        $this->table = 'users';
        $this->users_info = 'users_info';
        $this->users_spread = 'users_spread';
        $this->account = 'account';
        $this->users_qq_bind = 'users_qq_bind';
        $this->users_weibo_bind = 'users_weibo_bind';
        $this->users_sign = 'users_sign';
        $this->users_award_notice = 'users_award_notice';
        $this->content = 'content';
        $this->users_shipping_address = 'users_shipping_address';
        $this->users_alter = 'users_alter';
    }
    
    function get($uid){
        $this->db->select($this->table.'.*,'.$this->users_info.'.*,'.$this->account.'.*');
        $this->db->from($this->table);
        $this->db->join($this->users_info, $this->users_info.'.user_id = '.$this->table.'.user_id', 'left');
        $this->db->join($this->account, $this->account.'.user_id = '.$this->table.'.user_id', 'left');
        $this->db->where($this->table.'.user_id',$uid);
        return $this->db->get()->row_array();
    }
    
    function get_user($where){
        return $this->db->where($where)->get($this->table)->row_array();
    }
    function get_user_by_uid($uid=0){
        return $this->db->where('user_id',$uid)->get($this->table)->row_array();
    }
    
    function get_user_by_username($username){
        return $this->db->where('username',$username)->get($this->table)->row_array();
    }
    
    function get_user_by_mobile($mobile){
        return $this->db->where('mobile',$mobile)->get($this->table)->row_array();
    }
    
    function get_user_by_email($email){
        return $this->db->where('email',$email)->get($this->table)->row_array();
    }
    
    function insert($data){
        $this->db->insert($this->table, $data);
        if($this->db->affected_rows()){
            return $this->db->insert_id();
        }else{
            return 0;
        }
    }
    
    /**qq快捷绑定相关start*/
    function get_qq_bind($where=array()){
        return $this->db->where($where)->get($this->users_qq_bind)->row_array();
    }
    function insert_qq_bind($data){
        $this->db->insert($this->users_qq_bind, $data);
        if($this->db->affected_rows()){
            return $this->db->insert_id();
        }else{
            return 0;
        }
    }
    function del_qq_bind($user_id){
        $this->db->where("user_id",$user_id);
        $this->db->delete($this->users_qq_bind);
        return $this->db->affected_rows();
    }
    /**qq快捷绑定相关end*/
    /**Weibo快捷绑定相关start*/
    function get_weibo_bind($where=array()){
        return $this->db->where($where)->get($this->users_weibo_bind)->row_array();
    }
    function insert_weibo_bind($data){
        $this->db->insert($this->users_weibo_bind, $data);
        if($this->db->affected_rows()){
            return $this->db->insert_id();
        }else{
            return 0;
        }
    }
    function del_weibo_bind($user_id){
        $this->db->where("user_id",$user_id);
        $this->db->delete($this->users_weibo_bind);
        return $this->db->affected_rows();
    }
    /**Weibo快捷绑定相关end*/
    
    function update($data){
        return $this->db->where('user_id',$this->session->userdata('id'))->update($this->table, $data);
    }
    
    function user_update($data,$uid){
        return $this->db->where('user_id',$uid)->update($this->table, $data);
    }
    
    function user_info_update($data,$uid){
        return $this->db->where('user_id',$uid)->update($this->users_info, $data);
    }
    
    function insert_user_info($data)
    {
        $this->db->insert($this->users_info, $data);
        if($this->db->affected_rows()){
            return $this->db->insert_id();
        }else{
            return 0;
        }
    }
    //验证用户是否存在
    function get_user_flag($where){
        return $this->db->where($where)->get($this->table)->num_rows();
    }
    //通过ID验证用户是否存在
    function get_user_flag_by_uid($uid=0){
        return $this->db->where('user_id',$uid)->get($this->table)->num_rows();
    }
    //通过用户名验证是否存在用户
    function get_user_flag_by_username($username){
        return $this->db->where('username',$username)->get($this->table)->num_rows();
    }
    //用过email验证用户是否存在
    function get_user_flag_by_email($email){
        return $this->db->where('email',$email)->get($this->table)->num_rows();
    }
    //通过手机号验证用户是否存在
    function get_user_flag_by_mobile($mobile){
        return $this->db->where('mobile',$mobile)->get($this->table)->num_rows();
    }
    
    //获取5级父类推广id
    function get_5_parents($user_id){
        $parents = array();
        $sql = "select parent_id from tan_users_spread where user_id = $user_id";
        $result = $this->db->query($sql)->result();
        if($result){
            $parent = $result[0];//一级父类
            array_push($parents, $parent->parent_id);
            $sql = "select parent_id from tan_users_spread where user_id = ".$parent->parent_id;
            $result = $this->db->query($sql)->result();
            if($result){
                $parent = $result[0];//二级父类
                array_push($parents, $parent->parent_id);
                $sql = "select parent_id from tan_users_spread where user_id = ".$parent->parent_id;
                $result = $this->db->query($sql)->result();
                if($result){
                    $parent = $result[0];//三级父类
                    array_push($parents, $parent->parent_id);
                    $sql = "select parent_id from tan_users_spread where user_id = ".$parent->parent_id;
                    $result = $this->db->query($sql)->result();
                    if($result){
                        $parent = $result[0];//四级父类
                        array_push($parents, $parent->parent_id);
                        $sql = "select parent_id from tan_users_spread where user_id = ".$parent->parent_id;
                        $result = $this->db->query($sql)->result();
                        if($result){
                            $parent = $result[0];//五 级父类
                            array_push($parents, $parent->parent_id);
                        }
                    }
                }
            }
        }
        return $parents;
    }
    
    function get_user_sign($where){
        return $this->db->select('*')->from($this->users_sign)->where($where)->order_by('create_time desc')->get()->result_array();
    }
    
    function insert_user_sign($data){
        $this->db->insert($this->users_sign, $data);
        if($this->db->affected_rows()){
            return $this->db->insert_id();
        }else{
            return 0;
        }
    }
    
    //获取未读的站内信
    function get_no_read_sms($user_id){
        $this->db->select('*');
        $this->db->from($this->users_award_notice);
        $this->db->where(array("user_id"=>$user_id,"is_del"=>0,"is_see"=>0));
        $this->db->order_by('create_time','desc');
        $query = $this->db->get();
        return $query->num_rows();
    }
    
    //获取未读的系统公告
    function get_no_read_notice($user_id){
        $user_id = $user_id ? $user_id : 0;
        $sql = "SELECT count(*) count FROM tan_content 
                WHERE cate_id = (SELECT id FROM tan_content_category WHERE code='report') AND status = 1 AND id NOT IN (SELECT report_id FROM tan_content_report_status WHERE user_id = $user_id)";
        return $this->db->query($sql)->row_array();
    }
    //获取收货人地址信息
    function get_shipping_address_list($where){
        $this->db->select("*");
        $this->db->from($this->users_shipping_address);
        $this->db->where($where);
        $this->db->order_by('create_time','desc');
        return $this->db->get()->result_array();
    }
    //获取收货人的地址记录总数
    function get_shipping_address_count($where){
        $this->db->select("*");
        $this->db->from($this->users_shipping_address);
        $this->db->where($where);
        $this->db->order_by('create_time','desc');
        return $this->db->get()->num_rows();
    }
    //插入收货地址
    function insert_address($data){
        $this->db->insert($this->users_shipping_address, $data);
        if($this->db->affected_rows()){
            return $this->db->insert_id();
        }else{
            return 0;
        }
    }
    
    //更新收货地址
    function update_address($data,$id){
        return $this->db->where('id',$id)->update($this->users_shipping_address, $data);
    }
    //用户信息修改标记
    function users_alter($user_id,$type){
        if(!$type){
            $type="isupdate";
        }
        $this->db->select("*");
        $this->db->from($this->users_alter);
        $this->db->where(array("user_id"=>$user_id));
        if($this->db->get()->num_rows()==0){
            $this->db->insert($this->users_alter,array("user_id"=>$user_id,$type=>1));
        }else{
            $this->db->where('user_id',$user_id)->update($this->users_alter,array($type=>1));
        }
    }
}
